
var showCheckBox = false;//是否checkbox
var valueContainerId = "nodeid";//存放checkbox值的input控件的ID,配合你页面中定义的input控件的id

function loadNode(clickedObj){
    var uri = "ajaxTree.jsp";
    //var uri = "testData.xml";
    $.ajax({
        type: "POST",
        url: uri,
        data: "nodeid=321123&reqtime=" + (new Date()).getTime(),
        dataType: "xml",
        success: function(treeNodesXmlData){
            makeTreeNode(treeNodesXmlData, clickedObj);
        }
    });
}

/**
 * 根据服务器返回的XML数据构建树节点
 * @param {Object} treeNodesXmlData 服务器返回的XML数据
 * @param {Object} clickedObj 被点击的节点
 */
function makeTreeNode(treeNodesXmlData, clickedObj){
    var treeNodesAmount = $(treeNodesXmlData).find('treenode').length;
    $(treeNodesXmlData).find('treenode').each(function(i){
        var newNodeHtml = getTreeNodeHtml($(this), (i == treeNodesAmount - 1));
        $('<li></li>').html(newNodeHtml).appendTo(clickedObj).one("click", function(){
            loadNode($(this));
        });
    });
}

/**
 * 生成树节点HTML代码的函数
 * @param {Object} jTreeNode 树节点对象（jquery对象）
 * @param {Object} isLastNode 是否是当前节点的最后一个子节点
 */
function getTreeNodeHtml(jTreeNode, isLastNode){
    var newNodeHtml = "";
    if (showCheckBox) {
        newNodeHtml = getTreeNodeHtmlByCheckBox(jTreeNode, isLastNode);
    }
    else {
        newNodeHtml = getTreeNodeHtmlNoCheckBox(jTreeNode, isLastNode);
    }
    return newNodeHtml;
}

/**
 * 生成树节点HTML代码的函数
 * 无checkbox显示需要的情况
 * @param {Object} jTreeNode
 * @param {Object} isLastNode
 */
function getTreeNodeHtmlNoCheckBox(jTreeNode, isLastNode){
    var node_id = jTreeNode.attr("id");
    var node_name = jTreeNode.attr("name");
    var node_parentid = jTreeNode.attr("parentid");
    var node_value = jTreeNode.attr("value");
    var node_text = jTreeNode.attr("text");
    var newNodeHtml = "";
    if (jTreeNode.attr("childNodesAmount") == "0") {
        //无子孙节点
        if (isLastNode) {
            //最后一个子节点
            newNodeHtml += '<span><img src="resource/images/style0/noChildrenLastNode.gif" border="0"/></span>';
        }
        else {
            newNodeHtml += '<span><img src="resource/images/style0/noChildrenMidNode.gif" border="0"/></span>';
        }
        newNodeHtml += '<span><img src="resource/images/style0/file.gif" border="0"/></span>';
    }
    else {
        //有子孙节点
        if (isLastNode) {
            //最后一个子节点
            newNodeHtml += '<img src="resource/images/style0/collapsedLastNode.gif" border="0" onclick="displayNode(this);"/>';
        }
        else {
            newNodeHtml += '<img src="resource/images/style0/collapsedMidNode.gif" border="0"/>';
        }
        newNodeHtml += '<img src="resource/images/style0/folder.gif" border="0"/>';
    }
    newNodeHtml += '&nbsp;<a href="www.sina.com">'+node_text+'</a>';
    return newNodeHtml;
}

/**
 * 生成树节点HTML代码的函数
 * 有checkbox显示需要的情况
 * @param {Object} jTreeNode
 * @param {Object} isLastNode
 */
function getTreeNodeHtmlByCheckBox(jTreeNode, isLastNode){
    var node_id = jTreeNode.attr("id");
    var node_name = jTreeNode.attr("name");
    var node_parentid = jTreeNode.attr("parentid");
    var node_value = jTreeNode.attr("value");
    var node_text = jTreeNode.attr("text");
    var newNodeHtml = "";

    return newNodeHtml;
}

/**
 * 这个是抽象方法。当点击树节点文字时的动作
 * @param {Object} obj 被点击对象
 */
function nodeText_click(obj){
//TODO;
	//alert(obj.id);
}

function resetValue(obj){
	resetValueForContainer(obj, valueContainerId);
}

//当点击checkbox后，为表单元素重新付值 obj :checkbox , valueContainerId : 存放checkbox值的input控件的ID
function resetValueForContainer(obj, valueContainerId){
	selectAllSon(obj);
	var c = document.getElementsByName(ystreecheckboxname);
	var tmp = "";
	for(var i=0;i<c.length;i++){
		if(c[i].checked){
			tmp += c[i].value + ",";
		}
	}
	tmp = tmp.substring(0,tmp.length-1);
	if(document.getElementById( valueContainerId ))
		document.getElementById( valueContainerId ).value=tmp;
	else if( document.getElementsByName( valueContainerId )){
		document.getElementsByName( valueContainerId )[0].value = tmp;
	}
}

function selectAllSon(obj){
	var checkedid = obj.id;
	var c = document.getElementsByName(ystreecheckboxname);
	for(var i=c.length-1;i>=0;i--){
		if(c[i].id == checkedid){
		//说明该节点和被点节点是同级
		
		}else if(c[i].id.indexOf(checkedid)==0){
		//说明该节点是被点节点的子孙节点
			c[i].checked = obj.checked;
		}else if(checkedid.indexOf( c[i].id )==0){
		//说明该节点是被点节点的祖先节点
		//alert(c[i].id);
			if(isMySonAllSelected( c[i].id )){
				c[i].checked = true;
			}else{
				c[i].checked = false;
			}
		}
	}
	
}

function isMySonAllSelected(currID){
	var f = true;
	var c = document.getElementsByName(ystreecheckboxname);
	for(var i=0;i<c.length;i++){
		if( c[i].id!=currID && c[i].id.indexOf(currID)==0 ){
			if(c[i].checked == false){
				f = false;
				break;
			}
		}
	}
	return f;
}

